﻿using ProjectScheduling.daos;
using System.Data;

namespace ProjectScheduling
{
    public partial class CheckProjectTypeForm : Form
    {
        public static CheckProjectTypeForm b = new();
        private DataTable t_all = new DataTable();
        public CheckProjectTypeForm()
        {
            InitializeComponent();
        }
        public Action<(int id, string name, int timehours)>? action;
        private void CheckProjectTypeForm_Shown(object sender, EventArgs e)
        {
            t_all = StaffDao.Base.ToTable("SELECT ID,Name,StandardWorkingHours FROM ProjectTypes");

            view_all.DataSource = t_all;
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            if (view_all.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择项目类型", "选择提示");
                return;
            }
            var row = view_all.SelectedRows[0];
            string toString(string id)
            {
                return row.Cells[id]?.Value?.ToString() ?? "";
            }
            int toInt(string id)
            {
                return int.TryParse(toString(id), out int v) ? v : 0;
            }
            action?.Invoke((id: toInt("b_1"), name: toString("b_2"), timehours: toInt("b_3")));
            Close();
        }
        
        private void view_all_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btn_ok_Click(sender, e);
        }
    }
}
